package com.iplant.aps.serviceimpl.dao.erp;

import com.alibaba.fastjson.JSON;
import com.iplant.aps.service.po.aps.APSBOMItem;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.service.po.sap.IT_OUTPUT;
import com.iplant.aps.service.po.sfc.SFCBOMTask;
import com.iplant.aps.service.po.sfc.SFCBOMTaskItem;
import com.iplant.aps.service.po.wms.WMSLineEdgeLibrary;
import com.iplant.aps.service.po.wms.WMSLineEdgeLibraryItem;
import com.iplant.aps.service.po.wms.WMSPickDemand;
import com.iplant.aps.service.po.wms.WMSPickDemandItem;
import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import com.iplant.aps.serviceimpl.utils.aps.APSConstans;
import com.iplant.aps.serviceimpl.utils.sap.ZIF_MM_GET_WMS281_WSStub.*;
import com.iplant.aps.shristool.CalendarDAO;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.namespace.QName;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ERPUtils extends BaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(ERPUtils.class);

    private static ERPUtils Instance = null;

    private ERPUtils() {
        super();
    }

    public static ERPUtils getInstance() {
        if (Instance == null)
            Instance = new ERPUtils();
        return Instance;
    }

    /**
     * ERP过账
     */
    public IT_OUTPUT ERPPosting(String HD_INPUT, String IT_INPUT, String IT_OUTPUT, OMSOrder wOMSOrder) {
        IT_OUTPUT wResult = new IT_OUTPUT();
        try {
            String wEndPoint = Configuration.readConfigString("erpPostingIp", "config/config");

            org.apache.axis.client.Service wService = new org.apache.axis.client.Service();
            Call wCall = (Call) wService.createCall();

            wCall.setUsername(Configuration.readConfigString("erpPostingUser", "config/config"));
            wCall.setPassword(Configuration.readConfigString("erpPostringPassword", "config/config"));

            wCall.setTargetEndpointAddress(wEndPoint);
            wCall.setOperationName(new QName("urn:sap-com:document:sap:rfc:functions", "ZFMM_GET_WMS281"));// 调用的方法名
            wCall.addParameter(new QName("urn:sap-com:document:sap:rfc:functions", "HD_INPUT"), XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            wCall.addParameter(new QName("urn:sap-com:document:sap:rfc:functions", "IT_INPUT"), XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            wCall.addParameter(new QName("urn:sap-com:document:sap:rfc:functions", "IT_OUTPUT"), XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            wCall.setReturnType(XMLType.XSD_STRING); // 返回值类型：String

            Object wRealResult = wCall.invoke(new Object[]{HD_INPUT, IT_INPUT, IT_OUTPUT});// 远程调用
//			wResult = CloneTool.Clone(wRealResult, IT_OUTPUT.class);
            // ①写入日志
            CalendarDAO.getInstance().WriteLogFile(HD_INPUT + "::" + IT_INPUT + "::" + JSON.toJSONString(wRealResult),
                    "反冲物料ERP过账", wOMSOrder, "");
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }
        return wResult;
    }

    /**
     * 获取erp过账表头
     */
    public String GetHD_INPUT(WMSPickDemand wDemand, WMSPickDemandItem wItem) {
        String wResult = "";
        try {
//			SimpleDateFormat wSDF = new SimpleDateFormat("yyyy-MM-dd");
//			String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());
//			ZSGET_GR281HD_IN wZSGET_GR281HD_IN = new ZSGET_GR281HD_IN(wItem.DemandNo, "2", wDemand.OrderID, "2",
//					wCurrentTime, "1900");
//
//			HD_INPUT wHD_INPUT = new HD_INPUT();
//			wHD_INPUT.item = new ArrayList<ZSGET_GR281HD_IN>();
//			wHD_INPUT.item.add(wZSGET_GR281HD_IN);
//
//			wResult = JSON.toJSONString(wZSGET_GR281HD_IN);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 获取erp过账明细
     */
    public String GetIT_INPUT(WMSPickDemand wDemand, WMSPickDemandItem wItem) {
        String wResult = "";
        try {
//			String wWBS = wItem.WBSNo;
//			if (wWBS.length() > 9) {
//				wWBS = wWBS.substring(0, 10);
//			}
//
//			List<ZSGET_GR281IT_IN> wItemList = new ArrayList<ZSGET_GR281IT_IN>();
//			ZSGET_GR281IT_IN wZSGET_GR281IT_IN = new ZSGET_GR281IT_IN(wItem.DemandNo, wDemand.OrderID, wItem.ID,
//					wDemand.PartCode, "", wItem.AssessmentType, wItem.MaterialNo, wItem.BatchNumber, wItem.FQTY,
//					wItem.UnitText, wWBS);
//			wItemList.add(wZSGET_GR281IT_IN);
//
//			IT_INPUT wIT_INPUT = new IT_INPUT();
//			wIT_INPUT.item = wItemList;
//
//			wResult = JSON.toJSONString(wZSGET_GR281IT_IN);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public String getTestHD_INPUT() {
        String wResult = "";
        try {
            Document wDocument = DocumentHelper.createDocument();
            Element wRoot = wDocument.addElement("HD_INPUT");
            Element item = wRoot.addElement("item");
            Element ZWMSDOCID = item.addElement("ZWMSDOCID");
            ZWMSDOCID.setText("1");
            Element ZSYSID = item.addElement("ZSYSID");
            ZSYSID.setText("2");
            Element ZBOMID = item.addElement("ZBOMID");
            ZBOMID.setText("9");
            Element ZSCLX = item.addElement("ZSCLX");
            ZSCLX.setText("2");
            Element BUDAT = item.addElement("BUDAT");
            BUDAT.setText("2022-04-29");
            Element WERKS = item.addElement("WERKS");
            WERKS.setText("1900");

            wResult = wDocument.asXML().trim();
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public String getTestIT_INPUT() {
        String wResult = "";
        try {
            Document wDocument = DocumentHelper.createDocument();
            Element wRoot = wDocument.addElement("IT_INPUT");
            Element item = wRoot.addElement("item");
            Element ZWMSDOCID = item.addElement("ZWMSDOCID");
            ZWMSDOCID.setText("1");
            Element ZBOMID = item.addElement("ZBOMID");
            ZBOMID.setText("9");
            Element ZBOMITEMID = item.addElement("ZBOMITEMID");
            ZBOMITEMID.setText("2140");
            Element USR00 = item.addElement("USR00");
            USR00.setText("JCJX-027");
            Element ZLGORT = item.addElement("ZLGORT");
            ZLGORT.setText("");
            Element BWTAR = item.addElement("BWTAR");
            BWTAR.setText("常规新件");
            Element MATNR = item.addElement("MATNR");
            MATNR.setText("M00000019662");
            Element ZWMSBATCH = item.addElement("ZWMSBATCH");
            ZWMSBATCH.setText("1");
            Element IMENGE = item.addElement("IMENGE");
            IMENGE.setText("20");
            Element MEINS = item.addElement("MEINS");
            MEINS.setText("1");
            Element ZSPSPEL1W = item.addElement("ZSPSPEL1W");
            ZSPSPEL1W.setText("GQ-A602.P");

            wResult = wDocument.asXML().trim();
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public String getIT_OUTPUT() {
        String wResult = "";
        try {
            Document wDocument = DocumentHelper.createDocument();
            Element wRoot = wDocument.addElement("IT_OUTPUT");
            Element item = wRoot.addElement("item");
            Element ZWMSDOCID = item.addElement("ZWMSDOCID");
            ZWMSDOCID.setText("");
            Element ZBOMID = item.addElement("ZOK");
            ZBOMID.setText("");
            Element ZBOMITEMID = item.addElement("ZMSG");
            ZBOMITEMID.setText("");

            wResult = wDocument.asXML().trim();
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public TABLE_OF_ZSGET_GR281HD_IN getTABLE_OF_ZSGET_GR281HD_IN(APSBOMItem wAPSBOMItem, SFCBOMTask wSFCBOMTask,
                                                                  SFCBOMTaskItem wSFCBOMTaskItem) {
        TABLE_OF_ZSGET_GR281HD_IN wResult = new TABLE_OF_ZSGET_GR281HD_IN();
        try {
            ZSGET_GR281HD_IN wZSGET_GR281HD_IN = new ZSGET_GR281HD_IN();
            Char20 wZWMSDOCID = new Char20();
            wZWMSDOCID.setChar20(String.valueOf(wAPSBOMItem.OrderNum));
            wZSGET_GR281HD_IN.setZWMSDOCID(wZWMSDOCID);

            Char1 wZSYSID = new Char1();
            wZSYSID.setChar1("2");
            wZSGET_GR281HD_IN.setZSYSID(wZSYSID);

            wZSGET_GR281HD_IN.setZBOMID(wSFCBOMTask.OrderID);

            Char1 wZSCLX = new Char1();
            wZSCLX.setChar1("2");
            wZSGET_GR281HD_IN.setZSCLX(wZSCLX);

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyy-MM-dd");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());
            Date10 wDate10 = new Date10();
            wDate10.setDate10(wCurrentTime);
            wZSGET_GR281HD_IN.setBUDAT(wDate10);

            Char4 wWERKS = new Char4();
            wWERKS.setChar4("1900");
            wZSGET_GR281HD_IN.setWERKS(wWERKS);

            wResult.addItem(wZSGET_GR281HD_IN);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public TABLE_OF_ZSGET_GR281HD_IN getTABLE_OF_ZSGET_GR281HD_IN(WMSPickDemandItem wItem, WMSPickDemand wDemand,
                                                                  OMSOrder wOMSOrder) {
        TABLE_OF_ZSGET_GR281HD_IN wResult = new TABLE_OF_ZSGET_GR281HD_IN();
        try {
            ZSGET_GR281HD_IN wZSGET_GR281HD_IN = new ZSGET_GR281HD_IN();
            Char20 wZWMSDOCID = new Char20();
            wZWMSDOCID.setChar20(String.valueOf(wItem.ID));
            wZSGET_GR281HD_IN.setZWMSDOCID(wZWMSDOCID);

            Char1 wZSYSID = new Char1();
            wZSYSID.setChar1("2");
            wZSGET_GR281HD_IN.setZSYSID(wZSYSID);

            wZSGET_GR281HD_IN.setZBOMID(wOMSOrder.ID);

            Char1 wZSCLX = new Char1();
            wZSCLX.setChar1("2");
            wZSGET_GR281HD_IN.setZSCLX(wZSCLX);

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());
            Date10 wDate10 = new Date10();
            wDate10.setDate10(wCurrentTime);
            wZSGET_GR281HD_IN.setBUDAT(wDate10);

            Char4 wWERKS = new Char4();
            wWERKS.setChar4("1900");
            wZSGET_GR281HD_IN.setWERKS(wWERKS);

            wResult.addItem(wZSGET_GR281HD_IN);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public TABLE_OF_ZSGET_GR281IT_IN getTABLE_OF_ZSGET_GR281IT_IN(APSBOMItem wAPSBOMItem, SFCBOMTask wSFCBOMTask,
                                                                  SFCBOMTaskItem wSFCBOMTaskItem,
                                                                  WMSLineEdgeLibrary wWMSLineEdgeLibrary) {
        TABLE_OF_ZSGET_GR281IT_IN wResult = new TABLE_OF_ZSGET_GR281IT_IN();
        try {
            ZSGET_GR281IT_IN wZSGET_GR281IT_IN = new ZSGET_GR281IT_IN();

            Char20 wZWMSDOCID = new Char20();
            wZWMSDOCID.setChar20(String.valueOf(wAPSBOMItem.OrderNum));
            wZSGET_GR281IT_IN.setZWMSDOCID(wZWMSDOCID);

            wZSGET_GR281IT_IN.setZBOMID(wSFCBOMTask.OrderID);

            wZSGET_GR281IT_IN.setZBOMITEMID(wAPSBOMItem.OrderNum);

            Char20 wUSR00 = new Char20();
            wUSR00.setChar20(wSFCBOMTask.PartCode);
            wZSGET_GR281IT_IN.setUSR00(wUSR00);

            Char4 wZLGORT = new Char4();
            wZLGORT.setChar4("1200");
            wZSGET_GR281IT_IN.setZLGORT(wZLGORT);

            Char10 wBWTAR = new Char10();
            wBWTAR.setChar10(wAPSBOMItem.AssessmentType);
            wZSGET_GR281IT_IN.setBWTAR(wBWTAR);

            Char18 wMATNR = new Char18();
            wMATNR.setChar18(wAPSBOMItem.MaterialNo);
            wZSGET_GR281IT_IN.setMATNR(wMATNR);

            Char20 wZWMSBATCH = new Char20();
            String wBatchNumber = wWMSLineEdgeLibrary.BatchNumber;
            if (StringUtils.isEmpty(wBatchNumber)) {
                SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
                wBatchNumber = wSDF.format(wWMSLineEdgeLibrary.ProductTime.getTime());
            }
            wZWMSBATCH.setChar20(wBatchNumber);
            wZSGET_GR281IT_IN.setZWMSBATCH(wZWMSBATCH);

            Quantum133 wIMENGE = new Quantum133();
            wIMENGE.setQuantum133(new BigDecimal(wAPSBOMItem.Number));
            wZSGET_GR281IT_IN.setIMENGE(wIMENGE);

            Unit3 wMEINS = new Unit3();
            wMEINS.setUnit3(wAPSBOMItem.UnitText);
            wZSGET_GR281IT_IN.setMEINS(wMEINS);

//            Char24 wZSPSPEL1W = new Char24();
//            wZSPSPEL1W.setChar24(wSFCBOMTask.WBSNo.substring(0, 9));
//            wZSGET_GR281IT_IN.setZSPSPEL1W(wZSPSPEL1W);

            Char24 wZSPSPEL1W = new Char24();
            wZSPSPEL1W.setChar24(wWMSLineEdgeLibrary.ProjectStockNo);
            wZSGET_GR281IT_IN.setZSPSPEL1W(wZSPSPEL1W);

            wResult.addItem(wZSGET_GR281IT_IN);
        } catch (Exception ex) {
            logger.error("getTABLE_OF_ZSGET_GR281IT_IN", ex);
        }
        return wResult;
    }

    public TABLE_OF_ZSGET_GR281IT_IN getTABLE_OF_ZSGET_GR281IT_IN(WMSPickDemandItem wItem, WMSPickDemand wDemand,
                                                                  OMSOrder wOMSOrder) {
        TABLE_OF_ZSGET_GR281IT_IN wResult = new TABLE_OF_ZSGET_GR281IT_IN();
        try {
            ZSGET_GR281IT_IN wZSGET_GR281IT_IN = new ZSGET_GR281IT_IN();

            Char20 wZWMSDOCID = new Char20();
            wZWMSDOCID.setChar20(String.valueOf(wItem.ID));
            wZSGET_GR281IT_IN.setZWMSDOCID(wZWMSDOCID);

            wZSGET_GR281IT_IN.setZBOMID(wOMSOrder.ID);

            wZSGET_GR281IT_IN.setZBOMITEMID(wItem.BOMItemNum);

            Char20 wUSR00 = new Char20();
            wUSR00.setChar20(wItem.PartCode);
            wZSGET_GR281IT_IN.setUSR00(wUSR00);

            Char4 wZLGORT = new Char4();
            wZLGORT.setChar4("1200");
            wZSGET_GR281IT_IN.setZLGORT(wZLGORT);

            Char10 wBWTAR = new Char10();
            wBWTAR.setChar10(wItem.AssessmentType);
            wZSGET_GR281IT_IN.setBWTAR(wBWTAR);

            Char18 wMATNR = new Char18();
            wMATNR.setChar18(wItem.MaterialNo);
            wZSGET_GR281IT_IN.setMATNR(wMATNR);

            Char20 wZWMSBATCH = new Char20();
            wZWMSBATCH.setChar20(wItem.BatchNumber);
            wZSGET_GR281IT_IN.setZWMSBATCH(wZWMSBATCH);

            Quantum133 wIMENGE = new Quantum133();
            wIMENGE.setQuantum133(new BigDecimal(wItem.FQTY));
            wZSGET_GR281IT_IN.setIMENGE(wIMENGE);

            Unit3 wMEINS = new Unit3();
            wMEINS.setUnit3(wItem.UnitText);
            wZSGET_GR281IT_IN.setMEINS(wMEINS);

            Char24 wZSPSPEL1W = new Char24();
            wZSPSPEL1W.setChar24(wOMSOrder.OrderNo.substring(0, 9));
            wZSGET_GR281IT_IN.setZSPSPEL1W(wZSPSPEL1W);

            wResult.addItem(wZSGET_GR281IT_IN);
        } catch (Exception ex) {
            logger.error("getTABLE_OF_ZSGET_GR281IT_IN", ex);
        }
        return wResult;
    }

    public TABLE_OF_ZSGET_GR281HD_IN getTABLE_OF_ZSGET_GR281HD_IN(APSBOMItem wAPSBOMItem,
                                                                  WMSPickDemand wWMSPickDemand,
                                                                  WMSPickDemandItem wWMSPickDemandItem) {
        TABLE_OF_ZSGET_GR281HD_IN wResult = new TABLE_OF_ZSGET_GR281HD_IN();
        try {
            ZSGET_GR281HD_IN wZSGET_GR281HD_IN = new ZSGET_GR281HD_IN();
            Char20 wZWMSDOCID = new Char20();
            wZWMSDOCID.setChar20(String.valueOf(wAPSBOMItem.OrderNum));
            wZSGET_GR281HD_IN.setZWMSDOCID(wZWMSDOCID);

            Char1 wZSYSID = new Char1();
            wZSYSID.setChar1("2");
            wZSGET_GR281HD_IN.setZSYSID(wZSYSID);

            wZSGET_GR281HD_IN.setZBOMID(wAPSBOMItem.OrderID);

            Char1 wZSCLX = new Char1();
            wZSCLX.setChar1("2");
            wZSGET_GR281HD_IN.setZSCLX(wZSCLX);

            SimpleDateFormat wSDF = new SimpleDateFormat("yyyy-MM-dd");
            String wCurrentTime = wSDF.format(Calendar.getInstance().getTime());
            Date10 wDate10 = new Date10();
            wDate10.setDate10(wCurrentTime);
            wZSGET_GR281HD_IN.setBUDAT(wDate10);

            Char4 wWERKS = new Char4();
            wWERKS.setChar4("1900");
            wZSGET_GR281HD_IN.setWERKS(wWERKS);

            wResult.addItem(wZSGET_GR281HD_IN);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public TABLE_OF_ZSGET_GR281IT_IN getTABLE_OF_ZSGET_GR281IT_IN(APSBOMItem wAPSBOMItem,
                                                                  WMSPickDemand wWMSPickDemand,
                                                                  WMSPickDemandItem wWMSPickDemandItem,
                                                                  WMSLineEdgeLibrary wWMSLineEdgeLibrary,
                                                                  WMSLineEdgeLibraryItem wWMSLineEdgeLibraryItem) {
        TABLE_OF_ZSGET_GR281IT_IN wResult = new TABLE_OF_ZSGET_GR281IT_IN();
        try {
            ZSGET_GR281IT_IN wZSGET_GR281IT_IN = new ZSGET_GR281IT_IN();

            Char20 wZWMSDOCID = new Char20();
            wZWMSDOCID.setChar20(String.valueOf(wAPSBOMItem.OrderNum));
            wZSGET_GR281IT_IN.setZWMSDOCID(wZWMSDOCID);

            wZSGET_GR281IT_IN.setZBOMID(wAPSBOMItem.OrderID);

            wZSGET_GR281IT_IN.setZBOMITEMID(wAPSBOMItem.OrderNum);

            Char20 wUSR00 = new Char20();
            wUSR00.setChar20(APSConstans.GetFPCPartCode(wAPSBOMItem.PartID));
            wZSGET_GR281IT_IN.setUSR00(wUSR00);

            Char4 wZLGORT = new Char4();
            wZLGORT.setChar4("1200");
            wZSGET_GR281IT_IN.setZLGORT(wZLGORT);

            Char10 wBWTAR = new Char10();
            wBWTAR.setChar10(wAPSBOMItem.AssessmentType);
            wZSGET_GR281IT_IN.setBWTAR(wBWTAR);

            Char18 wMATNR = new Char18();
            wMATNR.setChar18(wAPSBOMItem.MaterialNo);
            wZSGET_GR281IT_IN.setMATNR(wMATNR);

            Char20 wZWMSBATCH = new Char20();
            String wBatchNumber = wWMSLineEdgeLibrary.BatchNumber;
            if (StringUtils.isEmpty(wBatchNumber)) {
                SimpleDateFormat wSDF = new SimpleDateFormat("yyyyMMdd");
                wBatchNumber = wSDF.format(wWMSLineEdgeLibrary.ProductTime.getTime());
            }
            wZWMSBATCH.setChar20(wBatchNumber);
            wZSGET_GR281IT_IN.setZWMSBATCH(wZWMSBATCH);

            Quantum133 wIMENGE = new Quantum133();
            wIMENGE.setQuantum133(new BigDecimal(wWMSLineEdgeLibraryItem.FQTY).setScale(2, RoundingMode.HALF_UP));
            wZSGET_GR281IT_IN.setIMENGE(wIMENGE);

            Unit3 wMEINS = new Unit3();
            wMEINS.setUnit3(wAPSBOMItem.UnitText);
            wZSGET_GR281IT_IN.setMEINS(wMEINS);

//            Char24 wZSPSPEL1W = new Char24();
//            wZSPSPEL1W.setChar24(wWMSPickDemand.WBSNo.substring(0, 9));
//            wZSGET_GR281IT_IN.setZSPSPEL1W(wZSPSPEL1W);

            Char24 wZSPSPEL1W = new Char24();
            wZSPSPEL1W.setChar24(wWMSLineEdgeLibrary.ProjectStockNo);
            wZSGET_GR281IT_IN.setZSPSPEL1W(wZSPSPEL1W);

            wResult.addItem(wZSGET_GR281IT_IN);
        } catch (Exception ex) {
            logger.error("getTABLE_OF_ZSGET_GR281IT_IN", ex);
        }
        return wResult;
    }
}
